﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
namespace NetCoreCMS.Web.Areas.Admin.Controllers.Order
{
    public class Payment_EditController : ManageController
    {
        private const string WEB_VIEW = "~/Areas/Admin/Views/Order/payment_edit.cshtml";
        private string action = CMSEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;
        protected Payment model = new Payment();
        public IPaymentService _payment { get; set; }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (filterContext.Result != null)
                return;
            ChkAdminLevel("order_payment", CMSEnums.ActionEnum.View.ToString()); //检查权限
            ActionResult result = View(EDIT_RESULT_VIEW);
            string _action = Request.Query["action"];

            if (!string.IsNullOrEmpty(_action) && _action == CMSEnums.ActionEnum.Edit.ToString())
            {
                this.action = CMSEnums.ActionEnum.Edit.ToString();//修改类型
                int.TryParse(Request.Query["id"],out this.id);
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    filterContext.Result = result;
                    return;
                }
                if (!_payment.Exists(this.id))
                {
                    JscriptMsg("记录不存在或已被删除！", "back");
                    filterContext.Result = result;
                    return;
                }
            }
            ViewBag.Id = this.id.ToString();
            ViewBag.Action = this.action;
        }
        //
        // GET: /admin/Payment_Edit/

        public ActionResult Index()
        {
            ShowInfo(this.id);
            return View(WEB_VIEW);
        }

        [HttpPost]
        public ActionResult SubmitSave(string txtRemark)
        {
            ActionResult result = View(EDIT_RESULT_VIEW);
            if (action == CMSEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("order_payment", CMSEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id, txtRemark))
                {
                    JscriptMsg("保存过程中发生错误！", string.Empty);
                    return result;
                }
                JscriptMsg("修改支付平台成功！", "../payment_list/index");
            }
            else //添加
            {
                ChkAdminLevel("order_payment", CMSEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd(txtRemark))
                {
                    JscriptMsg("保存过程中发生错误！", string.Empty);
                    return result;
                }
                JscriptMsg("添加支付平台成功！", "../payment_list/index");
            }
            return result;
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            model = _payment.GetModel(_id);
            if (model is null)
                model = new Payment();
            ViewData["model"] = model;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd(string txtRemark)
        {
            bool result = false;
            Payment model = new Payment();

            model.title = Request.Form["txtTitle"].FirstOrDefault().Trim();
            model.img_url = Request.Form["txtImgUrl"].FirstOrDefault().Trim();
            model.remark = txtRemark;
            model.is_lock = Request.Form["cbIsLock"].FirstOrDefault().ToLower().IndexOf("true") >= 0 ? 0 : 1;
            model.sort_id = int.Parse(Request.Form["txtSortId"].FirstOrDefault().Trim());
            model.poundage_type = int.Parse(Request.Form["rblPoundageType"]);
            model.poundage_amount = decimal.Parse(Request.Form["txtPoundageAmount"].FirstOrDefault().Trim());
            model.api_path = Request.Form["txtApiPath"].FirstOrDefault().Trim();
            model.redirect_url = Request.Form["txtRedirectUrl"].FirstOrDefault().Trim();
            model.return_url = Request.Form["txtReturnUrl"].FirstOrDefault().Trim();
            model.notify_url = Request.Form["txtNotifyUrl"].FirstOrDefault().Trim();

            if (_payment.Add(model) > 0)
            {
                AddAdminLog(CMSEnums.ActionEnum.Add.ToString(), "添加支付平台:" + model.title); //记录日志
                result = true;
            }
            return result;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id, string txtRemark)
        {
            bool result = false;
            Payment model = _payment.GetModel(_id);

            model.title = Request.Form["txtTitle"].FirstOrDefault().Trim();
            model.img_url = Request.Form["txtImgUrl"].FirstOrDefault().Trim();
            model.remark = txtRemark;
            model.is_lock = Request.Form["cbIsLock"].FirstOrDefault().ToLower().IndexOf("true") >= 0 ? 0 : 1;
            model.sort_id = int.Parse(Request.Form["txtSortId"].FirstOrDefault().Trim());
            model.poundage_type = int.Parse(Request.Form["rblPoundageType"]);
            model.poundage_amount = decimal.Parse(Request.Form["txtPoundageAmount"].FirstOrDefault().Trim());
            model.api_path = Request.Form["txtApiPath"].FirstOrDefault().Trim();
            model.redirect_url = Request.Form["txtRedirectUrl"].FirstOrDefault().Trim();
            model.return_url = Request.Form["txtReturnUrl"].FirstOrDefault().Trim();
            model.notify_url = Request.Form["txtNotifyUrl"].FirstOrDefault().Trim();


            if (_payment.Update(model))
            {
                AddAdminLog(CMSEnums.ActionEnum.Edit.ToString(), "修改支付平台:" + model.title); //记录日志
                result = true;
            }

            return result;
        }
        #endregion
    }
}
